package org.shop.controller.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Complains;
import org.shop.entity.Merchant;
import org.shop.entity.Orders;
import org.shop.entity.Users;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.ComplainsService;
import org.shop.service.MerchantService;
import org.shop.service.OrdersService;
import org.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class WebComplainsController {

	private static Logger logger = LogManager.getLogger(WebComplainsController.class);
	
	@Autowired
	private ComplainsService complainsService;
	@Autowired
	private MerchantService merchantService;
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private UserService userService;
	
	/**
	 * 获取投诉建议列表
	 * @param req
	 * @return
	 */
	@RequestMapping("web/getComplainsList")
	@ResponseBody
	public Map<String,Object> getComplainsList(HttpServletRequest req){
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> paramMap = new HashMap<String,Object>();
		String selectName = req.getParameter("selectName");
	 	String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
		try {
			paramMap.put("startPage", Integer.parseInt(currentPage));
			paramMap.put("pageSize", Integer.parseInt(pageSize));
			paramMap.put("selectName", selectName);
			paramMap.put("respondTargerId", ud.getUserId());
			map = complainsService.getComplainsInfoList(paramMap);
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return map;
	}
	
	
	/**
	 * 投诉建议回复(管理员仲裁)
	 * @param req
	 * @return
	 */
	@RequestMapping("web/replyComplains")
	@ResponseBody
	public int replyComplains(HttpServletRequest req) {
		int num = 0;
		Long complainId = Long.parseLong(req.getParameter("complainId"));
		String respondContent = req.getParameter("respondContent");
		try {
			Complains complains = new Complains();
			complains.setComplainId(complainId);
			complains.setRespondContent(respondContent);
			complains.setRespondTime(new Date());
			complains.setComplainStatus((byte) 3);
			num = complainsService.updateByPrimaryKeySelective(complains );
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
			num = 0;
		}
		return num;
	}
	
	/**
	 * 查看仲裁和回复
	 * @param req
	 * @return
	 */
	@RequestMapping("web/lookComplains")
	@ResponseBody
	public Map<String,Object> lookComplains(HttpServletRequest req) {
		Map<String,Object> map = new HashMap<String,Object>();
		Long complainId = Long.parseLong(req.getParameter("complainId"));
		try {
			Complains complainsInfo = complainsService.selectByPrimaryKey(complainId);
			if(complainsInfo.getGoalType()==0) {
				Merchant selectByPrimaryKey = merchantService.selectByPrimaryKey(complainsInfo.getGoalId());
				map.put("name", selectByPrimaryKey.getMerchantName());
			}else if(complainsInfo.getGoalType()==1) {
				Orders selectByPrimaryKey = ordersService.selectByPrimaryKey(complainsInfo.getGoalId());
				map.put("name", selectByPrimaryKey.getOrderNo());
			}
			Users selectByPrimaryKey = userService.selectByPrimaryKey(complainsInfo.getUserId());
			map.put("userNme", selectByPrimaryKey.getUserName());
			map.put("complainsInfo", complainsInfo);
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
			map.put("complainsInfo", null);
		}
		return map;
	}
	/**
	 * 获取目标
	 * @param req
	 * @return
	 */
	@RequestMapping("web/getComplains")
	@ResponseBody
	public Map<String,Object> getComplains(HttpServletRequest req) {
		Map<String,Object> map = new HashMap<String,Object>();
		Long goalId = Long.parseLong(req.getParameter("goalId"));
		int type = Integer.parseInt(req.getParameter("type"));
		try {
			if(type==0) {
				Merchant selectByPrimaryKey = merchantService.selectByPrimaryKey(goalId);
				map.put("name", selectByPrimaryKey.getMerchantName());
			}else if(type==1) {
				Orders selectByPrimaryKey = ordersService.selectByPrimaryKey(goalId);
				map.put("name", selectByPrimaryKey.getOrderNo());
			}
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return map;
	}
}
